import { createRouter, createWebHistory } from 'vue-router'
import HomePage from '@/pages/HomePage.vue'
import UserLoginPage from '@/pages/user/UserLoginPage.vue'
import UserRegisterPage from '@/pages/user/UserRegisterPage.vue'
import UserManagerPage from '@/pages/admin/UserManagerPage.vue'
import AppChatPage from '@/pages/app/AppChatPage.vue'
import AppManagePage from '@/pages/admin/AppManagePage.vue'
import AppEditPage from '@/pages/app/AppEditPage.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: '主页',
      component: HomePage,
    },
    {
      path: '/user/login',
      name: '用户登录',
      component: UserLoginPage,
    },
    {
      path: '/user/register',
      name: '用户注册',
      component: UserRegisterPage,
    },
    {
      path: '/admin/userManage',
      name: '用户管理',
      component: UserManagerPage,
      meta: {
        requiresAuth: true,
        requiresAdmin: true,
      },
    },
    {
      path: '/admin/appManage',
      name: '应用管理',
      component: AppManagePage,
      meta: {
        requiresAuth: true,
        requiresAdmin: true,
      },
    },
    {
      path: '/app/chat/:id',
      name: '应用对话',
      component: AppChatPage,
      meta: {
        requiresAuth: true,
      },
    },
    {
      path: '/app/create',
      name: '创建应用',
      component: AppEditPage,
      meta: {
        requiresAuth: true,
      },
    },
    {
      path: '/app/edit/:id',
      name: '编辑应用',
      component: AppEditPage,
      meta: {
        requiresAuth: true,
      },
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'NotFound',
      redirect: '/',
    },
  ],
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 这里可以添加权限验证逻辑
  // 例如检查用户是否登录，是否有管理员权限等

  // 检查是否需要登录
  if (to.meta.requiresAuth) {
    // 这里应该检查用户登录状态
    // 暂时跳过验证，实际项目中需要从store获取登录状态
    // const loginUserStore = useLoginUserStore()
    // if (!loginUserStore.loginUser.id) {
    //   next('/user/login')
    //   return
    // }
  }

  // 检查是否需要管理员权限
  if (to.meta.requiresAdmin) {
    // 这里应该检查用户是否为管理员
    // const loginUserStore = useLoginUserStore()
    // if (loginUserStore.loginUser.userRole !== 'admin') {
    //   next('/')
    //   return
    // }
  }

  next()
})

export default router
